package com.baijiayun.liveuibase.widgets.toolbar;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import androidx.core.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baijiayun.livecore.ppt.util.ShapeUtils;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.base.BaseUIConstant;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.ShadowUtil;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.widgets.popupwindow.BaseAutoArrangePopupWindow;

import java.util.HashMap;

public abstract class BaseDrawTextSettingWindow extends BaseAutoArrangePopupWindow {

    private final View contentView;
    private boolean isTextStrength = false;
    private boolean isTextTilt = false;
    private final int colorsGroupId = 1;

    private TextView lastCheckedTv = null;
    private View textSizeContainer;
    private View textSettingContainer;
    private final ShadowUtil shadowUtil;

    public BaseDrawTextSettingWindow(Context context) {
        super(context);
        contentView = LayoutInflater.from(context).inflate(R.layout.uibase_window_draw_text_setting, null);
        init();

        setResSetsMergeColors(null, colorsGroupId, color -> onChangeColor(new ColorSelectData(
                BaseUIConstant.SelectSrc.Text, color)));

        generateView(contentView.findViewById(R.id.base_toolbar_text_setting_color_container));
        setShowShadow(false);

        createView(contentView, false);

        shadowUtil = ShadowUtil.Create(DisplayUtils.dip2px(context, 2)
                , DisplayUtils.dip2px(context, 24));
        ShadowUtil.setViewBoundShadow(textSizeContainer, shadowUtil);
        ShadowUtil.setViewBoundShadow(textSettingContainer);
    }

    private void init() {
        textSizeContainer = contentView.findViewById(R.id.base_toolbar_text_setting_sizes_container);
        textSettingContainer = contentView.findViewById(R.id.base_toolbar_text_setting_container);

        initViewDataHashMap();

        initDrawable();

        initView();

        textSizeContainer.setSelected(false);
        contentView.findViewById(R.id.base_toolbar_text_setting_text_size).setOnClickListener(v -> {
            textSizeContainer.setSelected(!textSizeContainer.isSelected());
            updateSizesContainerParams();
        });

        contentView.findViewById(R.id.base_toolbar_text_setting_bold).setOnClickListener(v -> {
            isTextStrength = !isTextStrength;
            ShapeUtils.isFontWeight = isTextStrength;
            ((ImageView) contentView.findViewById(R.id.base_toolbar_text_setting_bold)).setImageDrawable(
                    isTextStrength ? getDrawable(R.drawable.base_draw_text_strength_press, true)
                            : ContextCompat.getDrawable(context, R.drawable.base_draw_text_strength));
        });

        contentView.findViewById(R.id.base_toolbar_text_setting_tilt).setOnClickListener(v -> {
            isTextTilt = !isTextTilt;
            ShapeUtils.isFontItalic = isTextTilt;
            ((ImageView) contentView.findViewById(R.id.base_toolbar_text_setting_tilt)).setImageDrawable(
                    isTextTilt ? getDrawable(R.drawable.base_draw_text_tilt_press, true)
                            : ContextCompat.getDrawable(context, R.drawable.base_draw_text_tilt));
        });
    }

    private void initDrawable() {
        GradientDrawable settingContainerDrawable = new GradientDrawable();
        settingContainerDrawable.setColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_brand_container_color));
        settingContainerDrawable.setCornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius));
        settingContainerDrawable.setStroke(DisplayUtils.dip2px(context, 1)
                , ContextCompat.getColor(context, R.color.base_bg_stroke_10));
        textSettingContainer.setBackground(settingContainerDrawable);

        StateListDrawable sizeContainerDrawable = new StateListDrawable();
        GradientDrawable sizeContainerDrawableSel = new GradientDrawable();
        sizeContainerDrawableSel.setColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_brand_container_color));
        sizeContainerDrawableSel.setCornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius));
        sizeContainerDrawable.addState(new int[]{
                android.R.attr.state_selected
        }, sizeContainerDrawableSel);
        sizeContainerDrawable.addState(new int[]{
                -android.R.attr.state_selected
        }, null);
        textSizeContainer.setBackground(sizeContainerDrawable);
    }

    private void initView() {
        int[] textSizes = new int[]{12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 40, 60, 80};

        LinearLayout allContainer = contentView.findViewById(R.id.base_toolbar_text_setting_size_selector_sizes);

        for (int textSize : textSizes) {
            LinearLayout container = new LinearLayout(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    DisplayUtils.dip2px(context, 32), ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.START;
            params.setMarginStart(DisplayUtils.dip2px(context, 2));
            container.setLayoutParams(params);
            container.setGravity(Gravity.START);

            TextView textView = new TextView(context);
            textView.setLayoutParams(params);
            textView.setTextSize(20);
            textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            textView.setTypeface(Typeface.MONOSPACE);
            textView.setText(String.valueOf(textSize));
            textView.setTextColor(textSize == 20 ? ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color)
                    : getColor(R.color.base_assistant_text_color));
            container.addView(textView);

            container.setOnClickListener(v -> {
                if (lastCheckedTv != null) {
                    lastCheckedTv.setTextColor(getColor(R.color.base_assistant_text_color));
                }
                textView.setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color));
                onChangeTextSize(textSize);
                ((TextView) contentView.findViewById(R.id.base_toolbar_text_setting_size_selector_preview))
                        .setText(String.valueOf(textSize));
                lastCheckedTv = textView;
            });

            allContainer.addView(container);
        }

        updateSizesContainerParams();
    }

    protected abstract void onChangeColor(ColorSelectData colorSelectData);

    protected abstract void onChangeTextSize(int textSize);

    private void updateSizesContainerParams() {
        contentView.findViewById(R.id.base_toolbar_text_setting_sizes).setVisibility(
                textSizeContainer.isSelected() ? View.VISIBLE : View.GONE);
        contentView.findViewById(R.id.base_toolbar_text_setting_sizes_divider_line).setVisibility(
                textSizeContainer.isSelected() ? View.VISIBLE : View.GONE);
        if (shadowUtil != null && shadowUtil.getShadowView() != null) {
            shadowUtil.getShadowView().setVisibility(
                    textSizeContainer.isSelected() ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    protected void initViewDataHashMap() {
        HashMap<Integer, BaseAutoArrangePopupWindow.GroupViewData> groupViewDataHashMap = new HashMap<>();
        groupViewDataHashMap.put(colorsGroupId,
                new BaseAutoArrangePopupWindow.GroupViewData(context, 130, 32, COMMON_CUSTOM_COLORS_ITEM_SIZE
                        , COMMON_CUSTOM_COLORS_ITEM_SIZE, 4));
        this.viewDataHashMap = groupViewDataHashMap;
    }

    @Override
    public void show(View anchor) {
        textSizeContainer.setSelected(false);
        updateSizesContainerParams();
        ShadowUtil.reInit(shadowUtil);
        showWithViewOfDirection(anchor, LEFT_BOTTOM);
    }
}
